package com.baidu.service;

import com.baidu.dao.NewsDao;
import com.baidu.entity.CommentBean;
import com.baidu.entity.NewsBean;
import com.baidu.utils.Page;

import java.util.Date;
import java.util.List;

/**
 * 创作时间：2019/9/23 14:34
 * 作者：李增强
 */
public class NewsService {
    NewsDao dao = new NewsDao();
    public List<NewsBean> getNewsList(String title, Page page) {
        String ssql="";
        if(title!=null&&!"".equals(title)){
            ssql+=" and title like '%"+title+"%'";
        }
        ssql+=" order by createdate desc";
        ssql+=" limit "+page.getStartRecord()+","+page.getPageSize();
        return dao.getNewsList(ssql);
    }

    public List<CommentBean> getCommentById(String newsid) {
        return dao.getCommentById(newsid);
    }

    public Integer getNewsCount(String title) {
        String ssql="";
        if(title!=null&&!"".equals(title)){
            ssql+=" and title like '%"+title+"%'";
        }
        return dao.getNewsCount(ssql);
    }

    public boolean deleteNewsById(String newsid) {
        try {
            dao.deleteCommentById(newsid);
            dao.deleteNewsById(newsid);
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    public void addComment(String newsid, String content, String author) {
        Date createdate = new Date();
        dao.addComment(newsid,content,author,createdate);
    }
}
